package view;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import lombok.Data;
import utils.Connect;
import utils.Group;
import utils.User;
import view.controller.MainWindowController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author
 */
@Data
public class MainWindow extends AbstractWindow {
    private Tab headTab;
    private User user;
    private List<User> friends;
    private List<Group> groups;
    private static MainWindow mainWindow=null;
    private Label groupName;
    private Label friendName;
    private  ObservableList<Object> friendList=null;
    private  ObservableList<Object> chatList=null;
    private  ObservableList<Object> groupList1=null;
    private ListView<Object> friendListView=null;
    public MainWindow(User user) throws IOException {


        //初始化好友消息列表与好友列表并赋值
        MainWindowController.friendList1= FXCollections.observableArrayList();
        MainWindowController.chatList1= FXCollections.observableArrayList();
        MainWindowController.groupList1=FXCollections.observableArrayList();
        this.friendList=MainWindowController.friendList1;
        this.chatList=MainWindowController.chatList1;
        this.groupList1=MainWindowController.groupList1;

        //初始加载好友列表
        MainWindowController.friendList1.add(this.setFriendList("image/mainWindow/information.png","个人信息"));
        MainWindowController.friendList1.add(this.setFriendList("image/mainWindow/addFriend.png","添加新好友"));
        MainWindowController.friendList1.add(this.setFriendList("image/mainWindow/friendApply.png","新的好友请求"));
        MainWindowController.friendList1.add(this.setFriendList("image/mainWindow/createGroup.png","创建群聊"));


        //获得当前账号的头像;
        Connect.getConnect().getHeadByAccount(user.getAccount());

        mainWindow=this;
        this.user=user;
        friends=new ArrayList<>();
        groups=user.getGroups();

        if(user.getFriends().size()!=0||user.getGroups().size()!=0) {
            initializeFriend();
        }
        MainWindowController.friends=this.friends;
        MainWindowController.groups=this.groups;
        setIcon();
        startWindow();
    }

    private void initializeFriend() throws IOException {
        //从数据库获取，通过String得到User
        Connect connect=Connect.getConnect();
        for (String friend : user.getFriends()) {
            connect.getUserByAccount(friend,1);
            try {
                connect=Connect.getConnect();
                connect.getHeadByAccount(friend);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (Group group : user.getGroups()) {
            MainWindowController.groupMessagePageList.add(FXCollections.observableArrayList());
            if(!connect.getHeadByAccount(group.getGroupAccount())){
                addGroupToGroupList(group);
            }
        }
        connect.getFriendMessageList(user);
        connect.getGroupMessageList(user);
    }

    public void addFriendToFriendList(User user) {
        addFriendToFriendList(user,this.friendList);
    }
    public void addFriendToFriendList(User user, ObservableList<Object> friendList) {
        ContextMenu menu = new ContextMenu();
        MenuItem item1 = new MenuItem("删除该好友");
        MenuItem item2 = new MenuItem("Preferences");
        menu.getItems().addAll(item1, item2);
        ImageView g = new ImageView("file:photo/"+user.getAccount()+".png");
        g.setFitHeight(50);
        g.setFitWidth(50);
        Label l = new Label(user.getName());
        HBox h = new HBox();
        h.getChildren().addAll(g, l);

        l.setContextMenu(menu);
        String str="删除该好友";
        menu.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MenuItem item = (MenuItem) event.getTarget();
                System.out.println(item.getText());
                if (str.equals(item.getText())) {
                    friendListView.getSelectionModel().clearSelection();
                    int num=MainWindowController.friends.indexOf(user);
                    MainWindowController.chatList1.remove(num);
                    MainWindowController.friendList1.remove(num+4);
                    MainWindowController.friends.remove(num);
                    MainWindowController.friendMessagePageList.remove(num);
                    if(MainWindowController.friendMessagePageList.size()==0){
                        if(MainWindowController.chooseFriendMessagePage!=null){
                            MainWindowController.chooseFriendMessagePage.clear();
                        }
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                friendName.setText(" ");
                            }
                        });
                    }
                    Connect connect=Connect.getConnect();
                    try {
                        connect.delete(MainWindow.getMainWindow().user,user);
                    } catch (IOException e) {
                        System.out.println("删除失败");
                    }

                }
            }
        });
        friendList.add(h);
    }

    public void addFriendToChatList(User user){
        addFriendToChatList(user,this.chatList);
    }
    public void addFriendToChatList(User user,ObservableList<Object> chatList){

        ImageView g = new ImageView("file:photo/"+user.getAccount()+".png");
        g.setFitHeight(50);
        g.setFitWidth(50);
        HBox h = new HBox();
        VBox v = new VBox();
        Label l = new Label(user.getName());
        l.setPrefHeight(25);
        v.getChildren().addAll(l);
        h.getChildren().addAll(g, v);
        chatList.add(h);
    }

    public void addGroupToGroupList(Group group){
        ContextMenu menu = new ContextMenu();
        MenuItem item1 = new MenuItem("查看群信息");
        MenuItem item2 = new MenuItem("退出该群");
        menu.getItems().addAll(item1, item2);

        ImageView g = new ImageView("file:photo/"+group.getGroupAccount()+".png");
        g.setFitHeight(50);
        g.setFitWidth(50);
        Label l = new Label(group.getGroupName());
        HBox h = new HBox();
        h.getChildren().addAll(g, l);

        l.setContextMenu(menu);
        menu.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MenuItem item = (MenuItem) event.getTarget();
                String exitGroup="退出该群";
                String lookUpGroup="查看群信息";
                if (exitGroup.equals(item.getText())) {
                    int index = MainWindowController.groups.indexOf(group);
                    MainWindowController.groupMessagePageList.remove(index);
                    if(MainWindowController.groupMessagePageList.size()==0){
                        MainWindowController.chooseGroupMessagePage.clear();
                       Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                groupName.setText(" ");
                        }
                    });
                    }

                    MainWindowController.groups.remove(index);
                    MainWindowController.groupList1.remove(index);
                    try {
                        Connect.getConnect().exitGroup(MainWindow.getMainWindow().getUser(),group);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if(lookUpGroup.equals(item.getText())){
                    new GroupInformation(group);
                }
            }
        });
        groupList1.add(h);
        System.out.println(groupName);
    }

    public static MainWindow getMainWindow(){
        return mainWindow;
    }

    public HBox setFriendList(String url,String information){
        ImageView imageView = new ImageView(url);
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Label label=new Label(information);
        HBox hBox=new HBox();
        hBox.getChildren().addAll(imageView,label);
        return hBox;
    }


    @Override
    public void startWindow() throws IOException {
        root = FXMLLoader.load(getClass().getResource("fxml/mainWindowView.fxml"));
        setTitle("AChat");
        setScene(new Scene(root));
        //move();
        show();
    }
}
